
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/tutorial/gotchas.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:54 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Gotchas &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="gotchas.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Basic Operations" href="basic_operations.html" />
    <link rel="prev" title="Introduction" href="intro.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="basic_operations.html" title="Basic Operations"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="Introduction"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Gotchas</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="gotchas">
<h1>Gotchas<a class="headerlink" href="#gotchas" title="Permalink to this headline">¶</a></h1>
<p>To begin, we should make something about SymPy clear.  SymPy is nothing more
than a Python library, like <code class="docutils literal notranslate"><span class="pre">NumPy</span></code>, <code class="docutils literal notranslate"><span class="pre">Django</span></code>, or even modules in the
Python standard library <code class="docutils literal notranslate"><span class="pre">sys</span></code> or <code class="docutils literal notranslate"><span class="pre">re</span></code>.  What this means is that SymPy does
not add anything to the Python language.  Limitations that are inherent in the
Python language are also inherent in SymPy.  It also means that SymPy tries to
use Python idioms whenever possible, making programming with SymPy easy for
those already familiar with programming with Python.  As a simple example,
SymPy uses Python syntax to build expressions.  Implicit multiplication (like
<code class="docutils literal notranslate"><span class="pre">3x</span></code> or <code class="docutils literal notranslate"><span class="pre">3</span> <span class="pre">x</span></code>) is not allowed in Python, and thus not allowed in SymPy.
To multiply <code class="docutils literal notranslate"><span class="pre">3</span></code> and <code class="docutils literal notranslate"><span class="pre">x</span></code>, you must type <code class="docutils literal notranslate"><span class="pre">3*x</span></code> with the <code class="docutils literal notranslate"><span class="pre">*</span></code>.</p>
<section id="symbols">
<span id="tutorial-gotchas-symbols"></span><h2>Symbols<a class="headerlink" href="#symbols" title="Permalink to this headline">¶</a></h2>
<p>One consequence of this fact is that SymPy can be used in any environment
where Python is available.  We just import it, like we would any other
library:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>This imports all the functions and classes from SymPy into our interactive
Python session.  Now, suppose we start to do a computation.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">NameError</span>: <span class="n">name &#39;x&#39; is not defined</span>
</pre></div>
</div>
<p>Oops! What happened here?  We tried to use the variable <code class="docutils literal notranslate"><span class="pre">x</span></code>, but it tells us
that <code class="docutils literal notranslate"><span class="pre">x</span></code> is not defined.  In Python, variables have no meaning until they
are defined.  SymPy is no different.  Unlike many symbolic manipulation
systems you may have used, in SymPy, variables are not defined automatically.
To define variables, we must use <code class="docutils literal notranslate"><span class="pre">symbols</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">x + 1</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">symbols</span></code> takes a string of variable names separated by spaces or commas,
and creates Symbols out of them.  We can then assign these to variable names.
Later, we will investigate some convenient ways we can work around this issue.
For now, let us just define the most common variable names, <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">y</span></code>, and
<code class="docutils literal notranslate"><span class="pre">z</span></code>, for use through the rest of this section</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>As a final note, we note that the name of a Symbol and the name of the
variable it is assigned to need not have anything to do with one another.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;b a&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">a</span>
</pre></div>
</div>
<p>Here we have done the very confusing thing of assigning a Symbol with the name
<code class="docutils literal notranslate"><span class="pre">a</span></code> to the variable <code class="docutils literal notranslate"><span class="pre">b</span></code>, and a Symbol of the name <code class="docutils literal notranslate"><span class="pre">b</span></code> to the variable
<code class="docutils literal notranslate"><span class="pre">a</span></code>.  Now the Python variable named <code class="docutils literal notranslate"><span class="pre">a</span></code> points to the SymPy Symbol named
<code class="docutils literal notranslate"><span class="pre">b</span></code>, and vice versa.  How confusing.  We could have also done something like</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">crazy</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;unrelated&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">crazy</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">unrelated + 1</span>
</pre></div>
</div>
<p>This also shows that Symbols can have names longer than one character if we
want.</p>
<p>Usually, the best practice is to assign Symbols to Python variables of the
same name, although there are exceptions:  Symbol names can contain characters
that are not allowed in Python variable names, or may just want to avoid
typing long names by assigning Symbols with long names to single letter Python
variables.</p>
<p>To avoid confusion, throughout this tutorial, Symbol names and Python variable
names will always coincide.  Furthermore, the word “Symbol” will refer to a
SymPy Symbol and the word “variable” will refer to a Python variable.</p>
<p>Finally, let us be sure we understand the difference between SymPy Symbols and
Python variables.  Consider the following:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="n">expr</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="nb">print</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
</pre></div>
</div>
<p>What do you think the output of this code will be?  If you thought <code class="docutils literal notranslate"><span class="pre">3</span></code>,
you’re wrong.  Let’s see what really happens</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">x + 1</span>
</pre></div>
</div>
<p>Changing <code class="docutils literal notranslate"><span class="pre">x</span></code> to <code class="docutils literal notranslate"><span class="pre">2</span></code> had no effect on <code class="docutils literal notranslate"><span class="pre">expr</span></code>.  This is because <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">2</span></code>
changes the Python variable <code class="docutils literal notranslate"><span class="pre">x</span></code> to <code class="docutils literal notranslate"><span class="pre">2</span></code>, but has no effect on the SymPy
Symbol <code class="docutils literal notranslate"><span class="pre">x</span></code>, which was what we used in creating <code class="docutils literal notranslate"><span class="pre">expr</span></code>.  When we created
<code class="docutils literal notranslate"><span class="pre">expr</span></code>, the Python variable <code class="docutils literal notranslate"><span class="pre">x</span></code> was a Symbol.  After we created, it, we
changed the Python variable <code class="docutils literal notranslate"><span class="pre">x</span></code> to 2.  But <code class="docutils literal notranslate"><span class="pre">expr</span></code> remains the same.  This
behavior is not unique to SymPy.  All Python programs work this way: if a
variable is changed, expressions that were already created with that variable
do not change automatically.  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;abc&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="s1">&#39;def&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">&#39;abcdef&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;ABC&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">&#39;abcdef&#39;</span>
</pre></div>
</div>
<aside class="sidebar">
<p class="sidebar-title">Quick Tip</p>
<p>To change the value of a Symbol in an expression, use <code class="docutils literal notranslate"><span class="pre">subs</span></code></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
</aside>
<p>In this example, if we want to know what <code class="docutils literal notranslate"><span class="pre">expr</span></code> is with the new value of
<code class="docutils literal notranslate"><span class="pre">x</span></code>, we need to reevaluate the code that created <code class="docutils literal notranslate"><span class="pre">expr</span></code>, namely, <code class="docutils literal notranslate"><span class="pre">expr</span> <span class="pre">=</span>
<span class="pre">x</span> <span class="pre">+</span> <span class="pre">1</span></code>.  This can be complicated if several lines created <code class="docutils literal notranslate"><span class="pre">expr</span></code>.  One
advantage of using a symbolic computation system like SymPy is that we can
build a symbolic representation for <code class="docutils literal notranslate"><span class="pre">expr</span></code>, and then substitute <code class="docutils literal notranslate"><span class="pre">x</span></code> with
values.  The correct way to do this in SymPy is to use <code class="docutils literal notranslate"><span class="pre">subs</span></code>, which will be
discussed in more detail later.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
</section>
<section id="equals-signs">
<span id="tutorial-gotchas-equals"></span><h2>Equals signs<a class="headerlink" href="#equals-signs" title="Permalink to this headline">¶</a></h2>
<p>Another very important consequence of the fact that SymPy does not extend
Python syntax is that <code class="docutils literal notranslate"><span class="pre">=</span></code> does not represent equality in SymPy.  Rather it
is Python variable assignment.  This is hard-coded into the Python language,
and SymPy makes no attempts to change that.</p>
<p>You may think, however, that <code class="docutils literal notranslate"><span class="pre">==</span></code>, which is used for equality testing in
Python, is used for SymPy as equality.  This is not quite correct either.  Let
us see what happens when we use <code class="docutils literal notranslate"><span class="pre">==</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">==</span> <span class="mi">4</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Instead of treating <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">1</span> <span class="pre">==</span> <span class="pre">4</span></code> symbolically, we just got <code class="docutils literal notranslate"><span class="pre">False</span></code>.  In
SymPy, <code class="docutils literal notranslate"><span class="pre">==</span></code> represents exact structural equality testing.  This means that
<code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span></code> means that we are <em>asking</em> if <span class="math notranslate nohighlight">\(a = b\)</span>.  We always get a <code class="docutils literal notranslate"><span class="pre">bool</span></code> as
the result of <code class="docutils literal notranslate"><span class="pre">==</span></code>.  There is a separate object, called <code class="docutils literal notranslate"><span class="pre">Eq</span></code>, which can be
used to create symbolic equalities</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Eq</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go">Eq(x + 1, 4)</span>
</pre></div>
</div>
<p>There is one additional caveat about <code class="docutils literal notranslate"><span class="pre">==</span></code> as well.  Suppose we want to know
if <span class="math notranslate nohighlight">\((x + 1)^2 = x^2 + 2x + 1\)</span>.  We might try something like this:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">==</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">False</span>
</pre></div>
</div>
<p>We got <code class="docutils literal notranslate"><span class="pre">False</span></code> again. However, <span class="math notranslate nohighlight">\((x + 1)^2\)</span> <em>does</em> equal <span class="math notranslate nohighlight">\(x^2 + 2x + 1\)</span>. What
is going on here?  Did we find a bug in SymPy, or is it just not powerful
enough to recognize this basic algebraic fact?</p>
<p>Recall from above that <code class="docutils literal notranslate"><span class="pre">==</span></code> represents <em>exact</em> structural equality testing.
“Exact” here means that two expressions will compare equal with <code class="docutils literal notranslate"><span class="pre">==</span></code> only if
they are exactly equal structurally.  Here, <span class="math notranslate nohighlight">\((x + 1)^2\)</span> and <span class="math notranslate nohighlight">\(x^2 + 2x + 1\)</span> are
not the same structurally. One is the power of an addition of two terms, and
the other is the addition of three terms.</p>
<p>It turns out that when using SymPy as a library, having <code class="docutils literal notranslate"><span class="pre">==</span></code> test for exact
structural equality is far more useful than having it represent symbolic
equality, or having it test for mathematical equality.  However, as a new
user, you will probably care more about the latter two.  We have already seen
an alternative to representing equalities symbolically, <code class="docutils literal notranslate"><span class="pre">Eq</span></code>.  To test if
two things are equal, it is best to recall the basic fact that if <span class="math notranslate nohighlight">\(a = b\)</span>,
then <span class="math notranslate nohighlight">\(a - b = 0\)</span>.  Thus, the best way to check if <span class="math notranslate nohighlight">\(a = b\)</span> is to take <span class="math notranslate nohighlight">\(a - b\)</span>
and simplify it, and see if it goes to 0.  We will learn <a class="reference internal" href="simplification.html#tutorial-simplify"><span class="std std-ref">later</span></a> that the function to do this is called <code class="docutils literal notranslate"><span class="pre">simplify</span></code>. This
method is not infallible—in fact, it can be <a class="reference external" href="https://en.wikipedia.org/wiki/Richardson%27s_theorem">theoretically proven</a> that it is impossible
to determine if two symbolic expressions are identically equal in
general—but for most common expressions, it works quite well.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">a</span> <span class="o">-</span> <span class="n">c</span><span class="p">)</span>
<span class="go">4*x</span>
</pre></div>
</div>
<p>There is also a method called <code class="docutils literal notranslate"><span class="pre">equals</span></code> that tests if two expressions are
equal by evaluating them numerically at random points.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</section>
<section id="two-final-notes-and">
<span id="tutorial-gotchas-final-notes"></span><h2>Two Final Notes: <code class="docutils literal notranslate"><span class="pre">^</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code><a class="headerlink" href="#two-final-notes-and" title="Permalink to this headline">¶</a></h2>
<p>You may have noticed that we have been using <code class="docutils literal notranslate"><span class="pre">**</span></code> for exponentiation instead
of the standard <code class="docutils literal notranslate"><span class="pre">^</span></code>.  That’s because SymPy follows Python’s conventions.  In
Python, <code class="docutils literal notranslate"><span class="pre">^</span></code> represents logical exclusive or.  SymPy follows this convention:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kc">True</span> <span class="o">^</span> <span class="kc">False</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kc">True</span> <span class="o">^</span> <span class="kc">True</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Xor</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">x ^ y</span>
</pre></div>
</div>
<p>Finally, a small technical discussion on how SymPy works is in order.  When
you type something like <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">1</span></code>, the SymPy Symbol <code class="docutils literal notranslate"><span class="pre">x</span></code> is added to the
Python int <code class="docutils literal notranslate"><span class="pre">1</span></code>.  Python’s operator rules then allow SymPy to tell Python
that SymPy objects know how to be added to Python ints, and so <code class="docutils literal notranslate"><span class="pre">1</span></code> is
automatically converted to the SymPy Integer object.</p>
<p>This sort of operator magic happens automatically behind the scenes, and you
rarely need to even know that it is happening.  However, there is one
exception.  Whenever you combine a SymPy object and a SymPy object, or a SymPy
object and a Python object, you get a SymPy object, but whenever you combine
two Python objects, SymPy never comes into play, and so you get a Python
object.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Integer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">&lt;class &#39;sympy.core.numbers.Integer&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">&lt;... &#39;int&#39;&gt;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>On running the example above in SymPy Live, (1+1) is wrapped
by Integer, so it does not show the correct output.</p>
</div>
<p>This is usually not a big deal. Python ints work much the same as SymPy
Integers, but there is one important exception:  division.  In SymPy, the
division of two Integers gives a Rational:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Integer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">Integer</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">1/3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Integer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">Integer</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">&lt;class &#39;sympy.core.numbers.Rational&#39;&gt;</span>
</pre></div>
</div>
<p>But in Python <code class="docutils literal notranslate"><span class="pre">/</span></code> represents either integer division or floating point
division, depending on whether you are in Python 2 or Python 3, and depending
on whether or not you have run <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span> 
<span class="go">0.5</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>On running the example above in SymPy Live, (1/2) is wrapped
by Integer, so it does not show the correct output.</p>
</div>
<p>To avoid this, we can construct the rational object explicitly</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">1/2</span>
</pre></div>
</div>
<p>This problem also comes up whenever we have a larger symbolic expression with
<code class="docutils literal notranslate"><span class="pre">int/int</span></code> in it.  For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span> 
<span class="go">x + 0.5</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>On running the example above in SymPy Live, (1/2) is wrapped
by Integer, so it does not show the correct output.</p>
</div>
<p>This happens because Python first evaluates <code class="docutils literal notranslate"><span class="pre">1/2</span></code> into <code class="docutils literal notranslate"><span class="pre">0.5</span></code>, and then
that is cast into a SymPy type when it is added to <code class="docutils literal notranslate"><span class="pre">x</span></code>.  Again, we can get
around this by explicitly creating a Rational:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">+</span> <span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">x + 1/2</span>
</pre></div>
</div>
<p>There are several tips on avoiding this situation in the <a class="reference internal" href="../gotchas.html#gotchas"><span class="std std-ref">Gotchas and Pitfalls</span></a>
document.</p>
</section>
<section id="further-reading">
<h2>Further Reading<a class="headerlink" href="#further-reading" title="Permalink to this headline">¶</a></h2>
<p>For more discussion on the topics covered in this section, see <a class="reference internal" href="../gotchas.html#gotchas"><span class="std std-ref">Gotchas and Pitfalls</span></a>.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Gotchas</a><ul>
<li><a class="reference internal" href="#symbols">Symbols</a></li>
<li><a class="reference internal" href="#equals-signs">Equals signs</a></li>
<li><a class="reference internal" href="#two-final-notes-and">Two Final Notes: <code class="docutils literal notranslate"><span class="pre">^</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code></a></li>
<li><a class="reference internal" href="#further-reading">Further Reading</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="intro.html"
                        title="previous chapter">Introduction</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="basic_operations.html"
                        title="next chapter">Basic Operations</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/tutorial/gotchas.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="basic_operations.html" title="Basic Operations"
             >next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="Introduction"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Gotchas</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/tutorial/gotchas.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:54 GMT -->
</html>